package com.xinrui.sunnyday.controller;

import com.alibaba.dubbo.config.ConsumerConfig;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import com.xinrui.sunnyday.constant.MessageConstant;
import com.xinrui.sunnyday.entity.PageResult;
import com.xinrui.sunnyday.entity.QueryPageBean;
import com.xinrui.sunnyday.entity.Result;
import com.xinrui.sunnyday.pojo.Setmeal;
import com.xinrui.sunnyday.service.SetmealService;
import com.xinrui.sunnyday.utils.QiNiuUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/setMeal")
public class SetmealController {

    @Reference
    private SetmealService setmealService;

    /**
     * 分页查询
     *
     * @param queryPageBean
     * @return
     */
    @PostMapping("/findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean) {
        PageInfo<Setmeal> pageInfo = setmealService.findPage(queryPageBean);
        PageResult pageResult = new PageResult(pageInfo.getTotal(), pageInfo.getList());
        return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS, pageResult);
    }

    /**
     * 图片上传
     *
     * @param imgFile
     * @return
     */
    @PostMapping("/upload")
    public Result upload(MultipartFile imgFile) {
        //获取图片文件名称
        String originalFilename = imgFile.getOriginalFilename();
        //截取
        String extension = originalFilename.substring(originalFilename.lastIndexOf("."));

        String fileName = UUID.randomUUID() + extension;

        try {
            QiNiuUtils.uploadViaByte(imgFile.getBytes(), fileName);

            HashMap<String, String> map = new HashMap<>();
            map.put("imgName", fileName);
            map.put("domain", QiNiuUtils.DOMAIN);
            return new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS, map);
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.PIC_UPLOAD_FAIL);
        }
    }

    /**
     * 新增
     *
     * @param setmeal
     * @param checkGroupIds
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody Setmeal setmeal, Integer[] checkGroupIds) {
        setmealService.add(setmeal, checkGroupIds);
        return new Result(true, MessageConstant.ADD_SETMEAL_SUCCESS);
    }

    /**
     * 编辑：查询套餐数据
     *
     * @param id
     * @return
     */
    @PostMapping("/findById")
    public Result findById(Integer id) {
        Setmeal setmeal = setmealService.findById(id);
        HashMap<String, Object> map = new HashMap<>();
        map.put("setmeal", setmeal);
        map.put("domain", QiNiuUtils.DOMAIN);
        map.put("imgName", setmeal.getImg());
        return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS, map);
    }

    @PostMapping("/findSetmealAndCheckGroupById")
    public Result findSetmealAndCheckGroupById(Integer id) {
        List<Integer> list = setmealService.findSetmealAndCheckGroupById(id);
        Integer[] ids = list.toArray(new Integer[0]);
        return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS, ids);
    }

    @PostMapping("/update")
    public Result update(@RequestBody Setmeal setmeal, Integer[] ids) {
        setmealService.update(setmeal, ids);
        return new Result(true, MessageConstant.EDIT_SETMEAL_SUCCESS);
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @PostMapping("/deleteById")
    public Result deleteById(Integer id) {
        setmealService.deleteById(id);
        return new Result(true, MessageConstant.DELETE_SETMEAL_SUCCESS);
    }
}
